package com.kunlong.fastdfs.service.impl;

import com.kunlong.fastdfs.dto.FastdfsFileInfoDto;
import com.kunlong.fastdfs.service.FastsdfsApiService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class FastdfsApiServiceImpl implements FastsdfsApiService {

	@Value("${file.fastdfs.server}")
	private String fileServer;

	@Override
	public Integer delete(String group, String remoteFilename)     {
		try {
			 return FastdfsService.delete(group,remoteFilename);

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public FastdfsFileInfoDto upload(String name, String ext, byte[] bytes, Map<String, String> props) {
		FastdfsFileInfoDto infoDTO = new FastdfsFileInfoDto();
		try {
			String[] path = FastdfsService.upload(name, ext, bytes, null);
			infoDTO.setExt(ext);
			infoDTO.setName(name);
			infoDTO.setPath(fileServer + "/" + path[0]+"/"+path[1]);
			infoDTO.setGroup(path[0]);
			infoDTO.setRemoteFileName(path[1]);
			infoDTO.setSize((long) bytes.length);
			return infoDTO;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public byte[] download(String groupName, String remoteFileName) {
		try {
			byte[] content = FastdfsService.download(groupName, remoteFileName);
			return content;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
